Python comprehensions

python
calmcodes
Seems like comprehensions is the quick way of using functional programming and readable code
Author

Jakob Johannesson

Published

August 2, 2024

Comprehensions

Code
import pandas as pd

df=pd.read_csv("https://raw.githubusercontent.com/TheEconomist/big-mac-data/master/output-data/big-mac-raw-index.csv")
Note

old_list = [2,4,2,4,5,3,6]

new_list = [i*2 for i in old_list]

new_list

Code
old_list = [2,4,2,4,5,3,6]
new_list =  [i*2 for i in old_list]
new_list
[4, 8, 4, 8, 10, 6, 12]

Add an if statement

Note

Adding the if-statement in the end of the comprehension

[i*2 for i in old_list if i%2==True]

The if-statement acts as a filter.

Code
old_list = [2,4,2,4,5,3,6]
new_list =  [i*2 for i in old_list if i%2==True]
new_list
[10, 6]

If-statements that changes the calculation logic

Code
old_list = [2,4,2,4,5,3,6]
new_list =  [i*2 if i > 3 else i * 3 for i in old_list if i%2==True]
new_list
[10, 9]

1st if statements adds logic, 2nd if statement filters the results.

Enumerate

Shorter and better than the for loop. Enumerate takes a string, finds the index and the value of it and adds it to a tuple. we can filter it to grab even indexed values in the string. Code below.

Code
[char for idx,char in enumerate('abcde') if idx % 2 == 0]
['a', 'c', 'e']

Let’s say we want to uppercase the ‘aeuio’ chars. This is a 3-step process, first we add the for-loop for idx,char in enumerate(old_list), then we add the filter if idx % 2 == 0, finally we add char.upper() if char in ‘aeuio’ else char. Full code below.

Code
old_list='abcde'
[char.upper() if char in 'aeuio' else char
for idx,char in enumerate(old_list)
if idx % 2 == 0]
['A', 'c', 'E']

Nested for loops

You may have multiple nested for loops, you can manage that with comprehensions too using code like this.

Code
[(i,j) for i in range(5) for j in range(i)]
[(1, 0),
 (2, 0),
 (2, 1),
 (3, 0),
 (3, 1),
 (3, 2),
 (4, 0),
 (4, 1),
 (4, 2),
 (4, 3)]

Add a filter in between loop 1 and loop 2 in order to filter in loop 1. If you want to filter loop 2, you add the filter after loop 2.

Use comprehensions on sets, tuples or dicts

Example with dict.

Code
{i: c for i, c in enumerate('abceabce')}
{0: 'a', 1: 'b', 2: 'c', 3: 'e', 4: 'a', 5: 'b', 6: 'c', 7: 'e'}
Warning

Careful with removing duplicate values. Switching index with key will drop duplicates.

Code of a switched index and key.

Code
{c: i for i, c in enumerate('abceabce')}
{'a': 4, 'b': 5, 'c': 6, 'e': 7}

it will take the one in the end of the list.

Unpacking a comprehension

Here we will get a index for each pair of key and value, which is smooth way of unpacking the data that is inside.

Code
arr = [('a', 1), ('b', 2), ('c', 2)]
[{key: value, 'i': idx} for idx, (key, value) in enumerate(arr)]
[{'a': 1, 'i': 0}, {'b': 2, 'i': 1}, {'c': 2, 'i': 2}]

Zipping

Code
d = {'a': 1, 'b': 2, 'c':3}
d.keys()
d.values()
dict_values([1, 2, 3])
Code
[(k,v) for k,v in d.items()]
[('a', 1), ('b', 2), ('c', 3)]

Let’s zip it!

Code
[(a,b) for a, b in zip([1,2],[3,4])]
[(1, 3), (2, 4)]

You may add more arrays if needed.